package com.ymatou.iapi.optrade.service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

import com.google.common.collect.Lists;
import com.ymatou.iapi.cardservice.service.BindingCardCall;
import com.ymatou.iapi.optrade.parameter.vo.CouponVo;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.trading.operate.parameter.OrderGroupVo;
import com.ymatou.iapi.trading.operate.parameter.OrderItemVo;
import com.ymatou.iapi.trading.operate.parameter.OrderVo;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.parameter.vo.PrepaidCardVo;
import com.ymttest.business.service.YMTTrading.TradingAllPromotion;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingProductVo;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.cardservice.CardServiceMapper;
import com.ymttest.utils.EnvSetup;

public class PrepaidCardService {

	public static CardServiceMapper cardServiceMapper = SqlMapperFactory
			.getCardServiceMapper();

	private static String cardId_sit1_1 = "10000003";

	private static String cardId_sit1_2 = "10000004";

	private static String cardId_uat_1 = "10000003";

	private static String cardId_uat_2 = "10000004";

	public static String cardId1, cardId2;

	static {

		if (EnvSetup.getEnv().equals("sit1")) {

			cardId1 = cardId_sit1_1;
			cardId2 = cardId_sit1_2;

		} else {
			cardId1 = cardId_uat_1;
			cardId2 = cardId_uat_2;
		}
	}

	public static void bindingCard(int userId, String userName) {

		//解绑卡
		cardServiceMapper.updateCardAsOrigByCardId(cardId1);

		BindingCardCall bindingCardCall = new BindingCardCall();

		bindingCardCall.call(cardId1, userId, userName, "auto");

	}

	public static void bindingCard(int userId, String userName, String cardId) {

		//解绑卡
		cardServiceMapper.updateCardAsOrigByCardId(cardId);

		BindingCardCall bindingCardCall = new BindingCardCall();

		bindingCardCall.call(cardId, userId, userName, "auto");

	}

	/**
	 * 查预付卡流水
	 * @param userId
	 */
	public static BigDecimal getCardEntry(int userId, String bizNo) {

		//预付卡流水
		List<Map<String, Object>> res = cardServiceMapper
				.selectCardEntryByUserId(userId, bizNo);

		if (CollectionUtils.isEmpty(res)) {
			return BigDecimal.ZERO;
		}

		return res.stream().map(m -> (BigDecimal) m.get("Amount"))
				.reduce(BigDecimal::add).get();

	}
	/**
	 * 预付卡 下单
	 * @return
	 */
	public static PlaceOrderResp placeOrder(boolean isUserCoupon) {

		PlaceOrderReq placeorderBean = PlaceOrderService.getDefaultOrderReq();

		int userId = placeorderBean.getUserId();

		OrderItemVo orderItemVo1 = PlaceOrderService.getDefaultOrderItemVo();

		OrderVo orderVo1 = PlaceOrderService.getDefaultOrderVo(userId);

		List<OrderVo> orderVoList = placeorderBean.getOrderGroups().get(0)
				.getOrders();

		orderVoList.get(0).getOrderItems().add(orderItemVo1);

		orderVoList.add(orderVo1);

		int sellerId2 = Integer.valueOf(EnvSetup.getData("SellerId2"));

		OrderGroupVo orderGroupVo2 = PlaceOrderService.getDefaultOrderGroupVo(
				userId, sellerId2);

		//OrderVo orderVo2 = PlaceOrderService.getDefaultOrderVo(userId);

		//orderGroupVo2.getOrders().add(orderVo2);

		placeorderBean.getOrderGroups().add(orderGroupVo2);

		PrepaidCardVo prepaidCardVo = new PrepaidCardVo();

		prepaidCardVo.setPrepaidAmount(BigDecimal.TEN);
		prepaidCardVo.setPriority(1);
		prepaidCardVo.setPrepaidCardNo(cardId1);

		PrepaidCardVo prepaidCardVo2 = new PrepaidCardVo();

		prepaidCardVo2.setPrepaidAmount(new BigDecimal(30));
		prepaidCardVo2.setPriority(2);
		prepaidCardVo2.setPrepaidCardNo(cardId2);

		placeorderBean.setPrepaidCardVos(Arrays.asList(prepaidCardVo,
				prepaidCardVo2));

		if (!isUserCoupon) {

			placeorderBean.setYmatouCoupon(null);

			placeorderBean.getOrderGroups().forEach(
					group -> group.setSellerCoupon(null));

		}

		return PlaceOrderService.callPlaceOrderService(placeorderBean);
	}

	/**
	 * 预付卡 下单
	 * @return
	 */
	public static TradingServiceResp placeOrder2(boolean isUserCoupon) {

		Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
		String sellerName = String.valueOf(EnvSetup.getData("autotest_sellerName1"));
		
		com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq();

		long userId = placeOrderReq.getUserId();
		
		com.ymatou.iapi.optrade.parameter.vo.OrderGroupVo orderGroupVo = TradingCallService
				.createOrderGroupVo(sellerId, sellerName);
		com.ymatou.iapi.optrade.parameter.vo.OrderVo orderVo1 = TradingCallService
				.createOrderVo();
		
		TradingProductVo productVo1 = TradingProductVo.createPro(sellerId, sellerName, 10);
		TradingProductVo productVo2 = TradingProductVo.createPro(sellerId, sellerName, 10);
		
		com.ymatou.iapi.optrade.parameter.vo.OrderItemVo orderItemVo1 = TradingCallService
				.createOrderItemVo(productVo1.getProductId(),
						productVo1.getCatalogId());
		com.ymatou.iapi.optrade.parameter.vo.OrderItemVo orderItemVo2 = TradingCallService
				.createOrderItemVo(productVo2.getProductId(),
						productVo2.getCatalogId());
		
		orderVo1.setOrderItems(Arrays.asList(orderItemVo1, orderItemVo2));
		
		com.ymatou.iapi.optrade.parameter.vo.OrderVo orderVo2 = TradingCallService
				.createOrderVo();
		
		TradingProductVo productVo3 = TradingProductVo.createPro(sellerId, sellerName, 10);
		
		com.ymatou.iapi.optrade.parameter.vo.OrderItemVo orderItemVo3 = TradingCallService
				.createOrderItemVo(productVo3.getProductId(),
						productVo3.getCatalogId());
		
		orderVo2.setOrderItems(Arrays.asList(orderItemVo3));
		
		orderGroupVo.setOrders(Arrays.asList(orderVo1, orderVo2));
		
		BigDecimal totalAmount = BigDecimal.ZERO;

		for (com.ymatou.iapi.optrade.parameter.vo.OrderVo order : orderGroupVo
				.getOrders()) {

			totalAmount = order
					.getOrderItems()
					.stream()
					.map(item ->

					item.getProductPrice()
							.multiply(new BigDecimal(item.getProductNum()))
							.add(item.getFreight())

					).reduce(BigDecimal.ZERO, BigDecimal::add).add(totalAmount);
		}

		com.ymatou.iapi.optrade.parameter.vo.PrepaidCardVo prepaidCardVo = new com.ymatou.iapi.optrade.parameter.vo.PrepaidCardVo();

		BigDecimal card1 = BigDecimal.ZERO;
		BigDecimal card2 = BigDecimal.ZERO;

		card1 = totalAmount.subtract(new BigDecimal(100)).subtract(
				BigDecimal.TEN);

		card2 = totalAmount.subtract(new BigDecimal(100)).subtract(card1);

		prepaidCardVo.setCardAmount(card1);

		prepaidCardVo.setCardNo(PrepaidCardService.cardId1);

		com.ymatou.iapi.optrade.parameter.vo.PrepaidCardVo prepaidCardVo2 = new com.ymatou.iapi.optrade.parameter.vo.PrepaidCardVo();

		prepaidCardVo2.setCardAmount(card2);

		prepaidCardVo2.setCardNo(PrepaidCardService.cardId2);

		placeOrderReq.setPrepaidCardVos(Arrays.asList(prepaidCardVo,
				prepaidCardVo2));

		if (isUserCoupon) {

			CouponVo sellerCoupon1 = TradingAllPromotion.createSellerCouponVo(
					userId, sellerId, new BigDecimal(30), BigDecimal.TEN);

			orderGroupVo.setSellerCoupon(sellerCoupon1);

			CouponVo ymatouCoupon = TradingAllPromotion.createYMTCouponVo(
					userId, new BigDecimal(60), BigDecimal.TEN);
			placeOrderReq.setYmatouCoupon(ymatouCoupon);

		}
		
		placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));

		return TradingCallService.callPlaceOrderService(placeOrderReq);

	}
	public static void main(String[] args) {

		int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));

		String userIdName = String.valueOf(EnvSetup.getData("BuyerName"));

		//PrepaidCardService.bindingCard(userId, userIdName);

		//PrepaidCardService.bindingCard(95, "lunchzhao", "10000005");

		/*	PrepaidCardService.cardServiceMapper.updateCardAmountByCardId(
					"10000004", 100000000);*/

	}
}
